package com.atguigu.write;

import com.atguigu.bean.Movie;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Index;

import java.io.IOException;
import java.util.HashMap;

/**
 * @author Lec
 * @date 2022/7/19 22:37
 */

public class Es01_SingleWrite {
    public static void main(String[] args) throws IOException {
        //1.创建客户端工厂
        JestClientFactory jestClientFactory = new JestClientFactory();

        //2.设置链接地址
        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();

        jestClientFactory.setHttpClientConfig(httpClientConfig);

        //3.获取客户端链接
        JestClient jestClient = jestClientFactory.getObject();

        //4.写入数据

//        HashMap<Object, Object> data = new HashMap<>();
//        data.put("id", "105");
//        data.put("name", "西游记");

        Movie movie = new Movie("106", "明朝那些事");

        Index index = new Index.Builder(movie)
                .index("movie_test220309")
                .type("_doc")
                .id("1006")
                .build();

        jestClient.execute(index);

        //关闭连接  虽然已经过期，但是也比close好用，因为close可能关不掉
        jestClient.shutdownClient();
    }

}
